/*
var mongoose = require('mongoose'),
    Schema = mongoose.Schema;
var settings = require('../config');

mongoose.connect(settings.db_url);
*/
var mongoose = require('../utils/MongooseConn');
var Schema = mongoose.Schema;
var settings = require('../config');
/**
 * 医师信息
 * */
/*var UserDoctor = new Schema({
    wcopenid : String,
    username : String,
    mobilephone : String,
    email : String,
    about : String
});
UserDoctor.index({
   wcopenid : 1
});
var userDoctor = mongoose.model("UserDoctor", UserDoctor);
*//*
 * 根据openid查询医师信息
 * *//*
userDoctor.prototype.findUserByOpenid = function (opneid, callback) {
    userDoctor.findOne({
        "wcopenid": opneid
    }, function (err, doc) {
        callback(err, doc);
    });
}*/

var MUserSchema = new Schema({
    user_name: String,
    user_passwd: String,
    phone:String,
    email:String,
    user_type: Number, //0默认商户，管理订单等 1代表管理员，定制微信账号
    appID:String,//应用ID
    appSecret:String,//应用密钥
    appStatus:Number,
    createtime:Date

});
var MUser = mongoose.model("MUser", MUserSchema);
MUser.prototype.saveMUser = function (callback) {
    this.save(function (err) {
        callback(err);
    })
}
MUser.prototype.findMUser = function (condition, callback) {
    MUser.findOne(condition, function (err, user) {
        callback(err, user);
    })
}
MUser.prototype.updatMUser = function (newpasswd, callback) {
    MUser.update({
        user_name: 'admin'
    }, {$set: {
        user_passwd: newpasswd
    }}, function (err) {
        callback(err);
    })
}
/**
 * 分页查询公众号用户列表
 * @param skip
 * @param callback
 */
MUser.prototype.getUsers = function (skip, callback) {
    var query = MUser.find();
    query.where('user_type', 0).skip((skip - 1) * settings.pre_page_row).limit(settings.pre_page_row);
    query.sort(
        {createtime: -1}
    )
    query.exec(function (err, result) {
        callback(err, result);
    })
};
/**
 * 统计公众号用户人数
 * @param callback
 */
MUser.prototype.getCount = function (callback) {
    var query = MUser.find();
    query.count(function (err, count) {
        callback(err, count-1);
    });
};

/**
 * 结算单
 */
var PayOffSchema = new Schema({
    payoff_count: Number,
    income: Number,
    payoff_totalcount: Number,
    payoff_date: Date,
    wcopenid: String,
    nickname: String,
    pay: {
        type: Number,
        default: 0
    }//0未付款，1已付款
})
var PayOff = mongoose.model('PayOff', PayOffSchema);
/**
 * 存储结算单
 * @param callback 回调函数
 */
PayOff.prototype.savePayoff = function (callback) {
    this.save(function (err) {
        callback(err);
    })
}
PayOff.prototype.getCount = function (callback) {
    var query = PayOff.find();
    query.count(function (err, count) {
        callback(err, count);
    })
}
PayOff.prototype.updatePayOff = function (pid, callback) {
    PayOff.update({
        _id: pid
    }, {
        $set: {
            pay: 1
        }
    }, function (err, numberAffected) {
        console.log('-----------------------------------------', numberAffected);
        callback(err);
    })
}
PayOff.prototype.findPayoff = function (skip, callback) {
    var query = PayOff.find();
    query.skip((skip - 1) * settings.pre_page_row).limit(settings.pre_page_row);
    query.sort(
        {payoff_date: -1}
    )
    query.exec(function (err, result) {
        callback(err, result);
    })
}
/*
 *微信用户
 */
var UserSchema = new Schema({
    wcopenid: String,
    nickname: String,
    weight: {
        type: Number,
        max: 1000
    },
    user_group: String,
    count: {
        type: Number,
        max: 50
    },
    random: Number
});
UserSchema.index({
    wcopenid: 1
});
var User = mongoose.model("User", UserSchema);
//更改用户所属组
User.prototype.updateUserGroup = function(openid, callback) {
    User.update({
        wcopenid: openid
    }, {
        $set: {
            user_group: "101"
        }
    }, function (err, numberAffected) {
        console.log('-----------------------------------------', numberAffected);
        callback(err);
    });
}
User.prototype.findUserByid = function (uid, callback) {
    User.findOne({
        "_id": uid
    }, function (err, doc) {
        callback(err, doc);
    });
}

/**
 *存储微信token
 */
var tokenSchema = new Schema({
    id: String,
    token: String,
    expireTime: String,
    refreshToken: String,
    date: Date
});
var Token = mongoose.model("Token", tokenSchema);
Token.prototype.saveToken = function (accesstoken, callback) {
    accesstoken.save(function (err) {
        callback(err);
        //debug("add new token", err);
    })
}
Token.prototype.findTokenById = function (tid, callback) {
    Token.findOne({
        id: tid
    }, function (err, token) {
        //debug("find token by id", token);
        callback(err, token);
    });
}
Token.prototype.removeTokenById = function (id, callback) {
    Token.remove({
        "id": id
    }, function (err, docs) {
        //debug("delete token", JSON.stringify(docs));
        callback(err);
    })
}
/**
 *更新OAuth
 */
// Token.prototype.updateToken = function(token, callback) {
// 	Token.update({
// 		"id": token.id
// 	}, {
// 		"token": token.token,
// 		"expireTime": token.expireTime,
// 		"refreshToken": token.refreshToken
// 	}, function(err, numberaffected) {
// 		callback(err, numberaffected);
// 	})
// }
/**
 *更新token含oauth123和token123
 */
Token.prototype.updateTokenById = function (id, options, callback) {
    Token.update({
        "id": id
    }, {
        $set: options
    }, function (err, numberaffected) {
        callback(err, numberaffected);
    })
}

module.exports.MUser = MUser;
module.exports.PayOff = PayOff;
module.exports.User = User
module.exports.Token = Token;